package com.kekezu.kppw.data;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalDb;

import org.apache.http.NameValuePair;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.widget.Toast;

import com.kekezu.kppw.bean.UserBean;
import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;

@SuppressWarnings("deprecation")
public class OtherData {
	static FinalDb db;

	/**
	 * 帮助
	 * 
	 * @param page
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getUserHelp(int page,
			Context context) {
		ArrayList<HashMap<String, Object>> taskList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/helpCenter?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("page", "" + page);
		params.put("token", "" + users.get(0).getToken());

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				String resData = jsonObject.getString("data");
				if (!resData.equals("[]")) {
					JSONObject jsondata = new JSONObject(resData);
					JSONArray jsonArray = jsondata.getJSONArray("data");

					if (jsonArray.length() > 0) {
						for (int i = 0; i < jsonArray.length(); i++) {
							HashMap<String, Object> map = new HashMap<String, Object>();
							JSONObject itemJsonObject = jsonArray
									.optJSONObject(i);

							map.put("title", itemJsonObject.getString("title"));
							map.put("content",
									itemJsonObject.getString("content"));

							taskList.add(map);
						}
					} else {
						Toast.makeText(context, "无数据", Toast.LENGTH_SHORT)
								.show();
					}
				} else {
					Toast.makeText(context, "无数据", Toast.LENGTH_SHORT).show();
				}

			} else {
				Toast.makeText(context, "请求异常", Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

	/**
	 * 意见反馈
	 * 
	 * @param strDesc
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getUserOpinion(
			String strDesc, Context context) {
		ArrayList<HashMap<String, Object>> taskList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/feedbackInfo";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("desc", strDesc));
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				Toast.makeText(context, "反馈意见提交成功", Toast.LENGTH_SHORT).show();
				((Activity) context).finish();
			} else if (resCode.equals("1001")) {
				Toast.makeText(context, "输入信息有误", Toast.LENGTH_SHORT).show();
			} else if (resCode.equals("1060")) {
				Toast.makeText(context, "反馈意见提交失败", Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

	// 附件上传
	public static String uploadImg(String strFile, Context context) {
		String strid = "";
		String urlString = ConfigInc.getServiceAdress(context) + "fileUpload";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		MultipartEntity multipart = new MultipartEntity();
		try {
			multipart.addPart("token", new StringBody(users.get(0).getToken()));
			multipart.addPart("file", new FileBody(new File(strFile)));

			String strJson = httpUtils.httpPost(urlString, multipart);
			JSONObject jsonObject = new JSONObject(strJson);

			Log.e("uploadimg", strJson);

			// 对返回的数据进行判断
			if (jsonObject.getString("code").equals("1000")) {
				JSONObject jsonData = jsonObject.getJSONObject("data");
				strid = jsonData.getString("id");
			} else {
				Toast.makeText(context, jsonObject.getString("message"),
						Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strid;
	}

	public static String getLoginUserId(Context context) {
		FinalDb db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		String strid = "userid";
		if (users.size() > 0) {
			strid = users.get(0).getUid();
		}

		return strid;
	}

	/**
	 * 获取服务端的版本信息
	 * 
	 */
	public static String getServiceVersion(Context context) {
		String urlString = ConfigInc.getServiceAdress(context)
				+ "androidVersion";
		HttpUtils httpUtils = HttpUtils.getInstance();

		String strVersion = null;
		try {
			String strJson = httpUtils.httpGet(urlString, context);
			JSONObject jsonObject = new JSONObject(strJson);

			if (jsonObject.getString("code").equals("1000")) {
				JSONObject jsonversion = new JSONObject(
						jsonObject.getString("data"));
				strVersion = jsonversion.getString("version");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strVersion;
	}

	/**
	 * 获取服务端的最新安装包下载地址
	 * 
	 */
	public static String getVersionDownloadUrl(Context context) {
		String urlString = ConfigInc.getServiceAdress(context) + "?do=version";
		HttpUtils httpUtils = HttpUtils.getInstance();

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("type", "android"));

		String strUrl = null;
		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);

			if (jsonObject.getString("ret").equals("0")) {
				JSONObject jsonversion = new JSONObject(
						jsonObject.getString("info"));

				strUrl = jsonversion.getString("url");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strUrl;
	}

	/**
	 * 对比版本
	 * 
	 * @return
	 */
	public static boolean checkVersion(Context context) {
		String serviceVersion = "";
		String clientVersion = "";

		// 服务器版本
		serviceVersion = getServiceVersion(context);
		// 客户端版本
		clientVersion = getVersion(context);

		Log.e("serviceVersion", "" + serviceVersion);
		Log.e("clientVersion", clientVersion);

		if (serviceVersion != "" && serviceVersion != null) {
			if (!clientVersion.equals(serviceVersion)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	/**
	 * 获取客户端版本
	 */
	public static String getVersion(Context context) {
		// 获取packagemanager的实例
		PackageManager packageManager = context.getPackageManager();
		// getPackageName()是你当前类的包名，0代表是获取版本信息
		PackageInfo packInfo = null;
		try {
			packInfo = packageManager.getPackageInfo(context.getPackageName(),
					0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return packInfo.versionName + "";
	}
}
